package cn.diancan.util.print;

import cn.diancan.util.DoubleArith;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ProductPrint3 implements Printable{

	private int height=40;
	private int width=220;
	private int x=0;
	private int y=-20;

	public int getHeight(){
		return height +draw(null);
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setHeight(int height) {
		this.height = height;
	}


	@Override
	public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

		if (page > 0) {
			return NO_SUCH_PAGE;
		}

		Graphics2D g2d = (Graphics2D) g;
		draw(g2d);

		return PAGE_EXISTS;
	}



	public int draw(Graphics2D g2d){
		if(null == g2d){
			BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
			g2d =image.createGraphics();
		}



		int fk=0;
		int nowHeight=0;
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		g2d.drawString(this.brandShopName, fk+55,  nowHeight=nowHeight+=16);
		nowHeight=nowHeight+5;
		g2d.setFont(new Font("Default", Font.PLAIN, 10));
		nowHeight=getNow(nowHeight);
		g2d.drawString("核销单:", fk,nowHeight);
		g2d.drawString(getOrderNumber(), fk+35,nowHeight);

		//读取第一张图片
		try {

			String path = new ProductPrint3().getClass().getClassLoader().getResource("").getPath();
			path= path.substring(0, path.length() - "WEB-INF/classes/".length()) + "images/res/print.png";
			System.out.println(path);

			File  fileOne  =  new File(path);
			BufferedImage  imageOne = ImageIO.read(fileOne);

			int mwidth=50;

			g2d.drawImage(imageOne,fk+40+70 ,0,mwidth,mwidth,null);
		} catch (IOException e) {
			e.printStackTrace();
		}


		nowHeight=getNow(nowHeight);
		g2d.drawString("营业员:", fk, nowHeight );
		g2d.drawString(getShopClerk(), fk+40, nowHeight );

		nowHeight=getNow(nowHeight);
		g2d.drawString("店铺编号:",fk,nowHeight);
		g2d.drawString(getShopNumber(),fk+50,nowHeight);


		String orderDate = getOrderDate();
		nowHeight=getNow(nowHeight);
		g2d.drawString("打印时间:",fk,nowHeight);
		g2d.drawString(orderDate.substring(2,orderDate.length()-3), fk+50,nowHeight);



		//g2d.drawString("VIP:", fk, nowHeight=getNow(nowHeight));
		nowHeight = getNow(nowHeight);

		g2d.drawString("地址:", fk, nowHeight);
		nowHeight =	drawSuperString(getShopAddr(), fk+40, nowHeight-10,11, g2d);

		nowHeight=getNow(nowHeight);

		nowHeight=getNow(nowHeight);
		g2d.drawString("菜品/品名", fk, nowHeight);
		g2d.drawString("数量*单价", fk+55, nowHeight);
		g2d.drawString("单位", fk+55+55, nowHeight);
		g2d.drawString("金额", fk+55+55+30, nowHeight);

		if( productTableVoList != null  && productTableVoList.size() >0 ){
			g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));
		}


		boolean b=true;

		for (int i = 0; i < getProductTableVoList().size(); i++) {
			ProductTableVo vo = getProductTableVoList().get(i);
			int maxNowHeight =  drawSuperString(vo.getGoodsName(), fk, nowHeight,4, g2d);
			int maxNowHeight2 = drawSuperString(vo.getQuantity()+"*"+vo.getRetailPrice(), fk+55, nowHeight,6, g2d);
			int maxNowHeight3 = drawSuperString(vo.getUnit(), fk+55+55, nowHeight,4, g2d);
			int maxNowHeight4 = drawSuperString(vo.getTurnover().toString(), fk+55+55+30, nowHeight,6, g2d);

			nowHeight = maxNowHeight>maxNowHeight2?maxNowHeight:maxNowHeight2;
			nowHeight = nowHeight>maxNowHeight3?nowHeight:maxNowHeight3;
			nowHeight = nowHeight>maxNowHeight4?nowHeight:maxNowHeight4;
		}

		if( productTableVoList2 != null  && productTableVoList2.size() >0 ){
			g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));
		}


		for (int i = 0; i < getProductTableVoList2().size(); i++) {

			ProductTableVo vo = getProductTableVoList2().get(i);
			int maxNowHeight =  drawSuperString(vo.getGoodsName(), fk, nowHeight,4, g2d);
			int maxNowHeight2 = drawSuperString(vo.getQuantity()+"*"+vo.getRetailPrice(), fk+55, nowHeight,6, g2d);
			int maxNowHeight3 = drawSuperString(vo.getUnit(), fk+55+55, nowHeight,4, g2d);
			int maxNowHeight4 = drawSuperString(vo.getTurnover().toString(), fk+55+55+30, nowHeight,6, g2d);

			nowHeight = maxNowHeight>maxNowHeight2?maxNowHeight:maxNowHeight2;
			nowHeight = nowHeight>maxNowHeight3?nowHeight:maxNowHeight3;
			nowHeight = nowHeight>maxNowHeight4?nowHeight:maxNowHeight4;
		}

		if( productTableVoList2 != null  && productTableVoList2.size() >0 ){
			g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));
			g2d.drawString("注：酒水无折扣", fk, nowHeight=getNow(nowHeight));
		}


		int p1=40;
		int p2=90;
		int p3=120;

		g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));
		//nowHeight=getNow(nowHeight);
		//g2d.drawLine(fk, nowHeight, getWidth(), nowHeight);;
		nowHeight=getNow(nowHeight);
		g2d.drawString("共计:", fk, nowHeight);
		g2d.drawString(getGongji(), fk+p1, nowHeight);

		if(null !=discount && !discount.equals(1F)){
			g2d.drawString("折扣:", fk+p2, nowHeight);
			g2d.drawString(getDiscount(), fk+p3, nowHeight);
		}else if(null !=youhui && youhui >0d){
			g2d.drawString("优惠:", fk+p2, nowHeight);
			g2d.drawString(getYouhui(), fk+p3, nowHeight);
		}


		g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));
		nowHeight=getNow(nowHeight);
		g2d.drawString("付款:", fk, nowHeight);
		g2d.drawString(getFukuan(), fk+p1, nowHeight);
		g2d.drawString("总数:", fk+p2, nowHeight);
		g2d.drawString(getZongshu(), fk+p3, nowHeight);
		g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));


/*		nowHeight=getNow(nowHeight);
		g2d.drawString("找零:", fk, nowHeight);
		g2d.drawString(getZhaolin(), fk+p1, nowHeight);
		g2d.drawString("积分:", fk+p2, nowHeight);
		g2d.drawString(getJifen(), fk+p3, nowHeight);
		g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));*/

		nowHeight=nowHeight+5;
		g2d.drawString(this.brandShopName2+"祝您用餐愉快", fk, nowHeight=getNow(nowHeight));




		//g2d.drawString(" ", fk, nowHeight=getNow(nowHeight));

		nowHeight=getNow(nowHeight);
		return nowHeight;
	}



	public int drawSuperString(String str,int x, int nowHeight, int _long,Graphics2D g2d){
		int maxNowHeight= nowHeight;
		int tempNowHeight =nowHeight;
		int hl = (str.length()/_long)+(str.length()%_long==0?0:1)-1;
		for (int j = 0; j <= hl; j++) {
			tempNowHeight = getNow(tempNowHeight);
			maxNowHeight  = tempNowHeight>maxNowHeight?tempNowHeight:maxNowHeight;
			g2d.drawString(str.substring(_long*j,j==hl?str.length():_long*(j+1)), x, tempNowHeight);
		}
		return maxNowHeight;
	}


	public int getNow(int nowHeight){
		nowHeight+=10;
		return nowHeight;
	}
	public int getLineNow(int nowHeight){
		nowHeight+=2;
		return nowHeight;
	}

	public ProductPrint3() {
		super();
	}


	private List<ProductTableVo> productTableVoList;
	private List<ProductTableVo> productTableVoList2;


	private String orderNumber;
	private String orderDate;
	private String shopNumber;
	private String brandShopName;
 	private String brandShopName2;
	private String shopClerk;
	private String shopAddr;
	private String vip;

	private Double gongji;
	private Double youhui;
	private Float discount;
	private Double fukuan;
	private Integer jifen;
	private Double zhaolin;
	private Integer zongshu;


	public ProductPrint3(List<ProductTableVo> productTableVoList, List<ProductTableVo> productTableVoList2,
                         String orderNumber, String orderDate, String shopNumber, String brandShopName, String brandShopName2,
                         String shopClerk, String shopAddr, String vip, Double gongji,
                         Double youhui, Float discount, Double fukuan, Integer jifen, Double zhaolin,

                         Integer zongshu) {
		super();
		this.productTableVoList = productTableVoList;
		this.productTableVoList2 = productTableVoList2;
		this.discount = discount;
		this.orderNumber = orderNumber;
		this.brandShopName = brandShopName;
		this.brandShopName2 =brandShopName2;
		this.orderDate = orderDate;
		this.shopNumber = shopNumber;
		this.shopClerk = shopClerk;
		this.shopAddr = shopAddr;
		this.vip = vip;
		this.gongji = gongji;
		this.youhui = youhui;
		this.fukuan = fukuan;
		this.jifen = jifen;
		this.zhaolin = zhaolin;
		this.zongshu = zongshu;
	}

	public List<ProductTableVo> getProductTableVoList() {
		if(null ==productTableVoList || productTableVoList.size()<=0){
			return new ArrayList<ProductTableVo>();
		}
		return productTableVoList;
	}

	public List<ProductTableVo> getProductTableVoList2() {
		if(null ==productTableVoList2 || productTableVoList2.size()<=0){
			return new ArrayList<ProductTableVo>();
		}
		return productTableVoList2;
	}



	public String getOrderDate() {
		if(null== orderDate){
			return "";
		}
		return orderDate;
	}

	public String getOrderNumber() {
		if(null== orderNumber){
			return "";
		}
		return orderNumber;
	}
	
	

	public String getShopAddr() {
		if(null== shopAddr){
			return "";
		}
		return shopAddr;
	}

	public String getShopNumber() {
		if(null== shopNumber){
			return "";
		}
		return shopNumber;
	}

	public String getShopClerk() {
		if(null== shopClerk){
			return "";
		}
		return shopClerk;
	}

	public String getVip() {
		if(null== vip){
			return "";
		}
		return vip;
	}

	public String getGongji() {
		if(null== gongji){
			return "";
		}
		return gongji.toString();
	}

	public String getYouhui() {
		if(null== youhui){
			return "";
		}
		return youhui.toString();
	}

	public String getFukuan() {
		if(null== fukuan){
			return "";
		}
		return fukuan.toString();
	}

	public String getJifen() {
		if(null== jifen){
			return "";
		}
		return jifen.toString();
	}

	public String getZhaolin() {
		if(null== zhaolin){
			return "";
		}
		return zhaolin.toString();
	}

	public String getZongshu() {
		if(null== zongshu){
			return "";
		}
		return zongshu.toString();
	}


	public String getDiscount() {
		if(null== discount){
			return "";
		}
		return Double.valueOf(DoubleArith.round(Double.valueOf(discount*10) ,1)).toString() ;
	}




}
